import { loginUser } from '@/service/user.js'
export default {
    state: {
        isLogin: !!localStorage.getItem('token')
            //布尔值的转换--
    },
    mutations: {
        setLoginState(state, val) {
            //--
            state.isLogin = val
        }
    },
    actions: {
        login({ commit }, userInfo) {
            return loginUser(userInfo).then(({ token }) => {
                //code  --token
                if (token) {
                    //登录成功
                    commit('setLoginState', true)
                    localStorage.setItem('token', token);
                    return true
                } else {
                    return false
                }
            })

            // axios.get('/api/login', { params: userInfo }).then(({ data }) => console.log(data))
            // axios.get('/api/login', { params: userInfo }).then(res => {
            //     const data = res.data
            //     console.log(data)
            // })

        },

        logout({ commit }) {
            commit('setLoginState', false),
                localStorage.removeItem('token')
        }
    }

}